<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['service:addUs:add']"-->
      <!--          >新增</el-button-->
      <!--        >-->
      <!--      </el-col>-->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
        :columns="columns"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="addUsList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        v-if="columns[0].visible"
        label="姓名"
        align="center"
        prop="name"
      />
      <el-table-column
        v-if="columns[1].visible"
        label="年龄"
        align="center"
        prop="age"
      />
      <el-table-column
        v-if="columns[2].visible"
        label="电话"
        align="center"
        prop="phonenumber"
      />
      <el-table-column
        v-if="columns[3].visible"
        label="身份证"
        align="center"
        prop="cardNo"
      />
      <el-table-column
        v-if="columns[4].visible"
        label="地区"
        align="center"
        prop="areaName"
      />
      <el-table-column
        v-if="columns[5].visible"
        label="详细地址"
        align="center"
        prop="addrInfo"
      />
      <el-table-column
        v-if="columns[6].visible"
        label="备注"
        align="center"
        prop="remark"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['service:addUs:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改加入我们对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="电话" prop="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入电话" />
        </el-form-item>
        <el-form-item label="身份证" prop="cardNo">
          <el-input v-model="form.cardNo" placeholder="请输入身份证" />
        </el-form-item>
        <el-form-item label="地区" prop="area">
          <el-cascader
            ref="cascaderRef"
            :props="{ label: 'deptName', value: 'deptId' }"
            :options="deptList"
            v-model="form.area"
            placeholder="请输入地区"
          />
        </el-form-item>
        <el-form-item label="详细地址" prop="addrInfo">
          <el-input v-model="form.addrInfo" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listAddUs,
  getAddUs,
  delAddUs,
  addAddUs,
  updateAddUs,
} from "@/api/service/addUs";
import { listDept } from "@/api/system/dept";
import { get, last } from "lodash";

export default {
  name: "AddUs",
  data() {
    return {
      deptList: [],
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 加入我们表格数据
      addUsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        age: undefined,
        phonenumber: undefined,
      },
      columns: [
        { key: 0, label: `姓名`, visible: true },
        { key: 1, label: `年龄`, visible: true },
        { key: 2, label: `电话`, visible: true },
        { key: 3, label: `身份证`, visible: true },
        { key: 4, label: `地区`, visible: true },
        { key: 5, label: `详细地址`, visible: true },
        { key: 6, label: `备注`, visible: true },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        age: [{ required: true, message: "年龄不能为空", trigger: "blur" }],
        phonenumber: [
          { required: true, message: "电话不能为空", trigger: "blur" },
        ],
        cardNo: [
          { required: true, message: "身份证不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDeptList();
  },
  methods: {
    /** 查询部门列表 */
    getDeptList() {
      this.loading = true;
      listDept().then((response) => {
        this.deptList = this.handleTree(response.data, "deptId");
        this.loading = false;
      });
    } /** 查询加入我们列表 */,
    getList() {
      this.loading = true;
      listAddUs(this.queryParams).then((response) => {
        this.addUsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        age: undefined,
        phonenumber: undefined,
        cardNo: undefined,
        area: undefined,
        addrInfo: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined,
        delFlag: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加加入我们";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getAddUs(id).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改加入我们";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          const result = {
            ...this.form,
            area: last(this.form.area),
            areaName: get(this.$refs.cascaderRef?.getCheckedNodes(), [
              0,
              "label",
            ]),
          };
          if (this.form.id != null) {
            updateAddUs(result)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addAddUs(result)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除加入我们编号为"' + ids + '"的数据项？')
        .then(() => {
          this.loading = true;
          return delAddUs(ids);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "service/addUs/export",
        {
          ...this.queryParams,
        },
        `addUs_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
